package com.medical.logback.response;

import lombok.Data;

/**
 * @Auther:gzf
 * @Date : 2022/06/23
 */
@Data
public class Result<T> {

    private int code;

    private String msg;

    private T data;

    /**
     * 请求成功时调用
     * @return
     */
    public static <T> Result<T> success(){
        return new Result<T>(ResultCode.SUCCESS);
    }

    public static <T> Result<T> success(T data){
        return new Result<T>(data);
    }

    public static <T> Result<T> error(ResultCode resultCode){
        return new Result<T>(resultCode);
    }
    public static <T> Result<T> error(int errCode,String errMsg){
        return new Result<T>(errCode,errMsg);
    }

    /**
     * 只传入数据默认成功，所以添加默认的code和msg
     * @param data
     */
    private Result(T data) {
        this.code=ResultCode.SUCCESS.getCode();
        this.msg=ResultCode.SUCCESS.getMsg();
        this.data=data;
    }

    private Result(ResultCode resultCode) {
        if(resultCode==null){
            return;
        }
        this.code=resultCode.getCode();
        this.msg=resultCode.getMsg();
    }
    private Result(int errCode ,String errMsg) {
        this.code=errCode;
        this.msg=errMsg;
    }

}
